import axios from 'axios';

const instance = axios.create({
  baseURL: '/api'
});

const noAuthUrls = [
  '/user/tokens',
  '/user/accounts',
  '/user/verification',
  '/user/auth/gitee'
];

instance.interceptors.request.use(config => {
  // 如果不是白名单接口，加 token
  if (!noAuthUrls.some(url => typeof url === 'string' ? config.url === url : url.test(config.url))) {
    const token = localStorage.getItem('token');
    console.log('Token:', token);  // 查看是否能正确获取 token
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
  }
  return config;
}, error => {
  return Promise.reject(error);
});

// 响应拦截器
instance.interceptors.response.use(
  response => response.data,
  error => {
    if (error.response) {
      return error.response.data;
    }
    return Promise.reject(error);
  }
);

export default instance;